package com.lyc.mapper;

import com.lyc.DTO.PostsDTO;
import com.lyc.pojo.PageBean;
import com.lyc.pojo.Post;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PostsMapper {

    // 添加文章
    @Insert("INSERT INTO posts(author_id, title, slug, summary, content, cover_image, status, comment_status, is_top, meta_data, published_at, created_at) " +
            "VALUES(#{authorId}, #{title}, #{slug}, #{summary}, #{content}, #{coverImage}, #{status}, #{commentStatus}, #{isTop}, #{metaData}, #{publishedAt}, NOW())")
    void addPost(PostsDTO pDTO);

    // 根据文章标题查询文章
    @Select("SELECT * FROM posts WHERE title = #{title}")
    Post findByPost(String title);

    // 根据文章ID查询文章
    @Select("SELECT * FROM posts WHERE id = #{id}")
    Post findByPostId(Long id);

    // 分页查询文章列表
    List<Post> getPostsList(Integer pageNum, Integer pageSize, Integer id);

    // 修改文章
    @Update("UPDATE posts SET  title = #{title}, slug = #{slug}, summary = #{summary}, content = #{content}, " +
            "cover_image = #{coverImage}, status = #{status}, comment_status = #{commentStatus}, is_top = #{isTop}, " +
            "meta_data = #{metaData}, published_at = #{publishedAt}, updated_at = NOW() WHERE id = #{id} ")
    void updatePosts(PostsDTO postsDTO);

    // 修改文章评论状态
    @Update("UPDATE posts SET comment_status = #{commentStatus} WHERE id = #{postId}")
    void updateCommentStatus(String commentStatus, Long postId);

    // 修改文章置顶状态
    @Update("UPDATE posts SET is_top = #{isTop} WHERE id = #{postId}")
    void updateIsTop(Integer isTop, Long postId);

    // 修改文章发布状态
    @Update("UPDATE posts SET status = #{status} WHERE id = #{postId}")
    void updateStatus(String status, Long postId);

    // 删除文章
    @Delete("DELETE FROM posts WHERE id = #{postId}")
    void deleteById(Long postId);



}
